# Stage 1: Build dependencies and extensions
FROM php:8.3.10-fpm-alpine AS build-stage


# 更新包并安装所需的系统依赖
#RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories

# 环境变量定义
ARG IMAGICK_VERSION=3.7.0

# 安装构建依赖及系统依赖
RUN apk --no-cache update \
    && apk --no-cache upgrade \
    && apk add --no-cache --virtual .build-deps \
    $PHPIZE_DEPS \
    gcc \
    g++ \
    autoconf \
    pcre-dev \
    imagemagick-dev \
    libtool \
    git \
    freetype-dev \
    libpng-dev \
    libjpeg-turbo-dev \
    libwebp-dev \
    libavif-dev \
    bzip2-dev \
    gettext-dev \
    icu-dev \
    libzip-dev \
    openldap-dev \
    postgresql-dev \
    rabbitmq-c-dev \
    oniguruma-dev \
    aspell-dev \
    libxml2-dev \
    && export CFLAGS="$PHP_CFLAGS" CPPFLAGS="$PHP_CPPFLAGS" LDFLAGS="$PHP_LDFLAGS" NPROC=$(getconf _NPROCESSORS_ONLN) \
    && docker-php-ext-configure gd --enable-gd --with-freetype --with-jpeg --with-webp --with-avif \
    && phpModules=" \
    apcu \
    bcmath \
    bz2 \
    calendar \
    exif \
    gd \
    gettext \
    opcache \
    pcntl \
    mysqli \
    pdo \
    pdo_mysql \
    pspell \
    shmop \
    soap \
    " \
    && mkdir -p /usr/src/php/ext/apcu \
    && curl -fsSL https://pecl.php.net/get/apcu/stable | tar xvz -C "/usr/src/php/ext/apcu" --strip 1 \
    && docker-php-ext-install -j${NPROC} $phpModules \
    && docker-php-ext-enable --ini-name 20-apcu.ini apcu \
    # 安装 redis 扩展
    && pecl install redis \
    && docker-php-ext-enable redis.so \
    # 安装 Imagick
    && cd /tmp \
    && curl -L -o /tmp/imagick.tar.gz https://github.com/Imagick/imagick/archive/refs/tags/${IMAGICK_VERSION}.tar.gz \
    && tar --strip-components=1 -xf /tmp/imagick.tar.gz \
    && phpize \
    && ./configure \
    && make \
    && make install \
    && echo "extension=imagick.so" > /usr/local/etc/php/conf.d/20-imagick.ini \
    && rm -rf /tmp/*

# Stage 2: Final stage
FROM php:8.3.10-fpm-alpine

LABEL org.opencontainers.image.authors="DeekeScript <miniphper@gmail.com>"

# 复制扩展
COPY --from=build-stage /usr/local/lib/php/extensions/ /usr/local/lib/php/extensions/
COPY --from=build-stage /usr/local/etc/php/conf.d/ /usr/local/etc/php/conf.d/

# 安装运行时依赖
RUN apk --no-cache update \
    && apk add --no-cache \
    aspell-dev \
    bash \
    ca-certificates \
    curl \
    freetype \
    imagemagick \
    imagemagick-libs \
    jpegoptim \
    libavif \
    libjpeg-turbo \
    libpng \
    libwebp-dev \
    libxml2-dev \
    libxslt-dev \
    libzip-dev \
    make \
    openssh \
    openssl \
    pngquant \
    rsync \
    shadow \
    sudo \
    wget \
    zip \
    && rm -rf /var/cache/apk/*

# 添加配置文件
ADD ./php.ini /usr/local/etc/php/php.ini
ADD ./php-fpm.d/zz-docker.conf /usr/local/etc/php-fpm.d/zz-docker.conf

# 安装 composer
RUN apk add --no-cache ca-certificates \
    && update-ca-certificates \
    && curl -sS https://getcomposer.org/installer | php \
    && mv composer.phar /usr/local/bin/ \
    && ln -s /usr/local/bin/composer.phar /usr/local/bin/composer

# 开放端口（如有必要）
# EXPOSE 9000

# 启动 PHP-FPM 服务
# CMD ["php-fpm"]
